package com.tuanchau.englishtooltip.responddata;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;
import com.tuanchau.englishtooltip.Global;
import com.tuanchau.englishtooltip.store.DatabaseController;
import com.tuanchau.englishtooltip.store.Word;

public class Responder {

	public static String doOnActionAdd(HttpServletRequest req) {
		String word = req.getParameter(Global.REQ_WORD);
		String detail = req.getParameter(Global.REQ_DETAIL);
		if (!checkWord(word, detail))
			return Global.RESP_FAIL + " : check word";
		try {
			Word w = new Word(word, detail);
			DatabaseController.putWord(w);
//			DatabaseController.putWord(word, type, mean);
		} catch (Exception e) {
			return Global.RESP_FAIL + " : add word";
		}
		return Global.RESP_SUCCESS;
	}

	public static String doOnActionAdds(HttpServletRequest req, PrintWriter resp) {
		try {
			String stream = req.getParameter(Global.REQ_STREAM);
			String[] arrWord = stream.split("\n");
			//resp.println("split word ok");
			String respord = "";
			int i = 0;
			for (String word : arrWord) {
				String[] arr = word.split("\t");
				//resp.println("arr length = " + arr.length);
				if (arr.length != 2)
					resp.println("#" + i + ": " + Global.RESP_FAIL + " \t:\t" + word);
				else {
					if (checkWord(arr[0], arr[1])) {
						try {
							DatabaseController.putWord(arr[0], arr[1]);
							resp.println("#" + i + ": " + arr[0] + " - "+ Global.RESP_SUCCESS);
//							respord += "#" + i + ": " + Global.RESP_SUCCESS
//									+ "\n";
						} catch (Exception e) {
							resp.println("#" + i + ": " + Global.RESP_FAIL + " : " + word);
//							respord += "#" + i + ": " + Global.RESP_FAIL + " \t:\t" + word
//									+ "\n";
						}
					} else {
						resp.println("#" + i + ": " + Global.RESP_FAIL + " \t:\t" + word);
//						respord += "#" + i + "\t" + Global.RESP_FAIL + "\t\n";
					}
				}
				i++;
			}
			return respord;
		} catch (Exception e) {
			return "exception here\r\n";
		}
	}

	public static String doOnActionGetWithWord(HttpServletRequest req) throws IOException {
		String word = req.getParameter(Global.REQ_WORD);
		if (word == null || word == "")
			return Global.DETAIL_NOWORD;
		Word w = DatabaseController.getEntity(word);
		if(w == null) {
			return Global.RESP_NOWORD;
		}
		else {
			return w.toXML();
		}
	}

//	public static String doOnActionGetWithWordAndType(HttpServletRequest req) {
//
//		String type = req.getParameter(Global.REQ_TYPE);
//		if (type == null || type == "")
//			return doOnActionGetWithWord(req);
//		String word = req.getParameter(Global.REQ_WORD);
//
//		if (word == null || word == "")
//			return Global.RESP_NOWORD;
//		List<Word> words = DatabaseController.getEntities(word, type);
//		return makeWordsRespord(words);
//	}
	
	public static String doResetDatabase(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		
		UserService userService = UserServiceFactory.getUserService();
        User user = userService.getCurrentUser();

        if (user != null) {
            if(user.getNickname().equalsIgnoreCase("tuanchau.ict")) {
            	DatabaseController.resetDatabase();
        		return "success";
            }
            else {
            	return "fail";
            }
        } else {
            resp.sendRedirect(userService.createLoginURL(req.getRequestURI() + "?action=140590"));
            return "fail";
        }
		
	}
	
	public static String doCounWordEntity() {
		return DatabaseController.countNumberOfRow();
	}

	private static boolean checkWord(String word, String detail) {
		if (word == null || detail == null || word == ""
				|| detail == "")
			return false;
		else
			return true;
	}

	private static String makeWordsRespord(List<Word> words) {
		String result = "<words><count>" + words.size() + "</count>";
		for (Word w : words) {
			result += w.toXML();
		}
		result += "</words>";
		return result;
	}
}
